package com.project.forlearning.service.impl;

import com.alibaba.fastjson.JSON;
import com.project.forlearning.constant.ResponseTool;
import com.project.forlearning.dao.BackReportResourceMapper;
import com.project.forlearning.pojo.ReportResource;
import com.project.forlearning.pojo.vo.ResponseInfo;
import com.project.forlearning.service.BackReportResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class BackReportResourceServiceImpl implements BackReportResourceService {
    @Autowired
    private BackReportResourceMapper backReportResourceMapper;

    @Override
    public String addReportResource(int resourceId, int userId, String reason, String reportDetails) {

        if(resourceId <= 0 || userId <= 0)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));

        if(backReportResourceMapper.addReportResource(resourceId, userId, reason, reportDetails))
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_SUCCESS.code, ResponseTool.ADD_SUCCESS.msg, true));

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_FAIL.code,  ResponseTool.ADD_FAIL.msg, false));
    }

    @Override
    public String queryReportResourceOne(int reportId) {

        if(reportId <= 0)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));

        ReportResource resource = backReportResourceMapper.queryReportResourceOne(reportId);
        if(resource == null)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_FAIL.code, ResponseTool.QUERY_FAIL.msg, false));

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, resource));
    }

    @Override
    public String queryReportResource() {

        List<ReportResource> reportResourceList = backReportResourceMapper.queryReportResource();
        if(reportResourceList == null)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS_ALL.code, ResponseTool.QUERY_SUCCESS_ALL.msg, new ArrayList()));

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, reportResourceList));
    }

    @Override
    public String deleteReportResource(int reportId) {
        if(reportId <= 0)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));

        if(backReportResourceMapper.deleteReportResource(reportId))
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_SUCCESS.code, ResponseTool.DELETE_SUCCESS.msg, true));

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_FAIL.code, ResponseTool.DELETE_FAIL.msg, false));
    }
}
